import {
  customeralterpriceslideorder,
  provideracceptalterprice,
  providerrejectalterprice,
  provideralterpriceslideorder,
  customeralterpriceslidehedgeorder,
  provideralterpriceslidehedgeorder,
  customeracceptalterslidebaseprice,
  customerrejectalterslidebaseprice,
  customeracceptalterconfirmfrimprice,
  customerrejectconfirmfrimprice
} from "@/api/order/modifyPrice";
// import {
//   customeracceptalterlineorderfirmprice,
//   customerrejectalterlineorderfirmprice,
//   customeracceptalterlineorderbaseprice,
//   customerrejectalterlineorderbaseprice
// } from "@/api/order-line";
import quotationDict from "@/dict/quotation";
import { isBuyer } from "@/util/common";

export const btnModifyPrice = [
  // 买家
  {
    name: "申请改价",
    clickEvent: "applyModifyPrice",
    flag: "applyModifyPrice"
  },
  {
    name: "申请改价",
    clickEvent: "applyModifyPriceHedge",
    flag: "applyModifyPriceHedge"
  },
  // 待生成合同修改价格 ******* start
  // 普通订单
  {
    name: "同意改价",
    clickEvent: "acceptSellerModifyBasePrice",
    flag: "acceptSellerModifyBasePrice"
  },
  {
    name: "拒绝改价",
    clickEvent: "rejectSellerModifyBasePrice",
    flag: "rejectSellerModifyBasePrice"
  },
  {
    name: "同意改价",
    clickEvent: "acceptSellerModifyFirmPrice",
    flag: "acceptSellerModifyFirmPrice"
  },
  {
    name: "拒绝改价",
    clickEvent: "rejectSellerModifyFirmPrice",
    flag: "rejectSellerModifyFirmPrice"
  },
  // // 线路订单
  // {
  //   name: "同意改价",
  //   clickEvent: "acceptSellerModifyBasePriceByLine",
  //   flag: "acceptSellerModifyBasePriceByLine"
  // },
  // {
  //   name: "拒绝改价",
  //   clickEvent: "rejectSellerModifyBasePriceByLine",
  //   flag: "rejectSellerModifyBasePriceByLine"
  // },
  // {
  //   name: "同意改价",
  //   clickEvent: "acceptSellerModifyFirmPriceByLine",
  //   flag: "acceptSellerModifyFirmPriceByLine"
  // },
  // {
  //   name: "拒绝改价",
  //   clickEvent: "rejectSellerModifyFirmPriceByLine",
  //   flag: "rejectSellerModifyFirmPriceByLine"
  // },
  // 待生成合同修改价格 ******* end
  // 卖家
  {
    name: "修改点价",
    clickEvent: "modifyPrice",
    flag: "modifyPrice"
  },
  {
    name: "修改点价",
    clickEvent: "modifyPriceHedge",
    flag: "modifyPriceHedge"
  },
  {
    name: "同意改价",
    clickEvent: "acceptModifyPrice",
    flag: "acceptModifyPrice"
  },
  {
    name: "拒绝改价",
    clickEvent: "rejectModifyPrice",
    flag: "rejectModifyPrice"
  }
];

export const modifyPrice = {
  data() {
    return {
      modifyPriceAmount: undefined
    };
  },
  methods: {
    // 设置默认价格
    setDefaultPrice(row) {
      let res =
        row.slidePriceType === quotationDict.detailPriceType.base.value
          ? row.basePrice
          : Number(row.basePrice) + Number(row.premium);
      this.modifyPriceAmount = res;
      return res;
    },
    // 买家申请改价
    applyModifyPrice(row) {
      this.setDefaultPrice(row);
      this.modifyPriceHandle(row, 0, customeralterpriceslideorder);
    },
    // 买家申请改价（套保）
    applyModifyPriceHedge(row) {
      this.setDefaultPrice(row);
      this.modifyPriceHandle(row, 1, customeralterpriceslidehedgeorder);
    },
    // 卖家修改价格
    modifyPrice(row) {
      this.setDefaultPrice(row);
      this.modifyPriceHandle(row, 2, provideralterpriceslideorder);
    },
    // 卖家修改价格（套保）
    modifyPriceHedge(row) {
      this.setDefaultPrice(row);
      this.modifyPriceHandle(row, 3, provideralterpriceslidehedgeorder);
    },
    // 提交价格修改
    modifyPriceHandle(row, type, request) {
      const h = this.$createElement;
      let _this = this;
      let content =
        type === 0 || type === 1
          ? "本次修改点价需要对方同意方可修改成功。"
          : "修改点价之前，请先和对方协商，避免产生误会。若确认已和对方协商，请在输入修改的价格。";
      this.$confirm({
        title: "修改点价价格",
        icon: "exclamation-circle",
        content: h("div", {}, [
          h("p", content),
          h("p", `原点价价格：${this.setDefaultPrice(row)}`),
          h("a-input", {
            style: {
              width: "260px",
              marginBottom: "1em"
            },
            attrs: {
              placeholder: "请输入价格"
            },
            domProps: {
              value: _this.modifyPriceAmount
            },
            on: {
              input: function(event) {
                _this.modifyPriceAmount = event.target.value;
              }
            }
          }),
          h(
            "div",
            {
              class: ["danger"]
            },
            `${isBuyer() ? "本订单剩余修改次数 " + row.alterPriceCount : ""}`
          )
        ]),
        onOk() {
          return new Promise((resolve, reject) => {
            // let request =
            //   type === 0
            //     ? customeralterpriceslideorder
            //     : provideralterpriceslideorder;
            request({
              orderId: row.id,
              version: row.version,
              targetPrice: _this.modifyPriceAmount
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type:
                    type === 0
                      ? "applyModifyPrice"
                      : type === 1
                      ? "applyModifyPriceHedge"
                      : type === 2
                      ? "modifyPrice"
                      : "modifyPriceHedge"
                });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        },
        onCancel() {
          _this.modifyPriceAmount = undefined;
        }
      });
    },
    // 卖家，同意改价
    acceptModifyPrice(data) {
      let _this = this;
      this.$confirm({
        title: "此操作将修改该订单价格, 是否继续?",
        onOk() {
          return new Promise((resolve, reject) => {
            provideracceptalterprice({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: "acceptModifyPrice"
                });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        }
      });
    },
    // 卖家，拒绝改价
    rejectModifyPrice(data) {
      providerrejectalterprice({
        orderId: data.id,
        version: data.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", { data: res, type: "rejectModifyPrice" });
      });
    },
    // 同意修改基价/含税单价
    acceptSellerModifyPrice(data, type, request) {
      let _this = this;
      this.$confirm({
        title: "此操作将修改该订单价格, 是否继续?",
        onOk() {
          return new Promise((resolve, reject) => {
            request({
              orderId: data.id,
              version: data.version
            })
              .then(res => {
                _this.$message.success("操作成功");
                _this.$emit("handleSuccess", {
                  data: res,
                  type: type
                });
                resolve();
              })
              .catch(() => {
                reject();
              });
          });
        }
      });
    },
    // 拒绝修改基价/含税单价
    rejectSellerModifyPrice(data, type, request) {
      request({
        orderId: data.id,
        version: data.version
      }).then(res => {
        this.$message.success("操作成功");
        this.$emit("handleSuccess", { data: res, type: type });
      });
    },
    // 买家同意修改基价
    acceptSellerModifyBasePrice(data) {
      this.acceptSellerModifyPrice(
        data,
        "acceptSellerModifyBasePrice",
        customeracceptalterslidebaseprice
      );
    },
    // 买家拒绝修改基价
    rejectSellerModifyBasePrice(data) {
      this.rejectSellerModifyPrice(
        data,
        "rejectSellerModifyBasePrice",
        customerrejectalterslidebaseprice
      );
    },
    // 买家同意修改含税单价
    acceptSellerModifyFirmPrice(data) {
      this.acceptSellerModifyPrice(
        data,
        "acceptSellerModifyFirmPrice",
        customeracceptalterconfirmfrimprice
      );
    },
    // 买家拒绝修改含税单价
    rejectSellerModifyFirmPrice(data) {
      this.rejectSellerModifyPrice(
        data,
        "rejectSellerModifyFirmPrice",
        customerrejectconfirmfrimprice
      );
    }
    // // 买家同意修改线路订单基价
    // acceptSellerModifyBasePriceByLine(data) {
    //   this.acceptSellerModifyPrice(
    //     data,
    //     "acceptSellerModifyBasePriceByLine",
    //     customeracceptalterlineorderbaseprice
    //   );
    // },
    // // 买家拒绝修改线路订单基价
    // rejectSellerModifyBasePriceByLine(data) {
    //   this.rejectSellerModifyPrice(
    //     data,
    //     "rejectSellerModifyBasePriceByLine",
    //     customerrejectalterlineorderbaseprice
    //   );
    // },
    // // 买家同意修改线路订单含税单价
    // acceptSellerModifyFirmPriceByLine(data) {
    //   this.acceptSellerModifyPrice(
    //     data,
    //     "acceptSellerModifyFirmPriceByLine",
    //     customeracceptalterlineorderfirmprice
    //   );
    // },
    // // 买家拒绝修改线路订单含税单价
    // rejectSellerModifyFirmPriceByLine(data) {
    //   this.rejectSellerModifyPrice(
    //     data,
    //     "rejectSellerModifyFirmPriceByLine",
    //     customerrejectalterlineorderfirmprice
    //   );
    // }
  }
};
